package com.juyu.miliao.api.dao.redis;

import com.juyu.miliao.common.constant.BillBeansGameKey;
import com.juyu.miliao.common.constant.CoolSummerActivitiesRedisKey;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;

import java.time.LocalDate;
import java.util.concurrent.TimeUnit;

/**
 *  用户领取暑期活动游戏奖励和个人当天获得寻豆的redis
 * @Auther: 卜加超
 * @Date: 2019/8/6 19:32
 * @Description:
 */

@Repository
public class SanGuoGameActivityRedisDao {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    /**
     * 获取 三国个人赢取寻豆
     * @param now
     * @param userId
     * @return
     */
    public long getSanGuoGameWinBeans(LocalDate now, Long userId){
        String key = BillBeansGameKey.getSanGuoGameWinBeansKey(now,userId);
        String s = stringRedisTemplate.opsForValue().get(key);
        long sanGuoGameWinBeans = 0;
        if(!StringUtils.isEmpty(s)){
            sanGuoGameWinBeans = Long.parseLong(s);
        }
        return sanGuoGameWinBeans;
    }

    /**
     * 添加用户领取奖励
     * @param now
     * @param userId
     * @param id
     */
    public long setIsGiveBeans(LocalDate now,long userId,Integer id){
        String key =  BillBeansGameKey.getIsGiveBeansKey(now,id);
        Long add = stringRedisTemplate.opsForSet().add(key, String.valueOf(userId));
        stringRedisTemplate.expire(key,10, TimeUnit.DAYS);
        return add;
    }

    /**
     * 判断用户是否领取过奖励
     * @param now
     * @param userId
     * @param id
     * @return
     */
    public boolean checkIsGiveBeans(LocalDate now,long userId,Integer id){
        String key =  BillBeansGameKey.getIsGiveBeansKey(now,id);
        return stringRedisTemplate.opsForSet().isMember(key,userId+"");

    }

    /**
     * 统计用户领取三国奖励
     */
    public void statisticsUserGetSanGuoRewards(long userId,LocalDate day,int sanGuoActiveId){
        String  key = BillBeansGameKey.getSanGuoActivitiesStatisticsGetRewardsKey(day,sanGuoActiveId);
        stringRedisTemplate.opsForHyperLogLog().add(key,userId+"");
        stringRedisTemplate.expire(key,10, TimeUnit.DAYS);
    }

    /**
     * 删除用户领取三国奖励redis
     * @param userId
     * @param localDate
     * @param sanGuoActiveId
     */
    public void removeUserGetSanGuoRewards(long userId, LocalDate localDate, int sanGuoActiveId) {
        String  key = BillBeansGameKey.getIsGiveBeansKey(localDate,sanGuoActiveId);
        stringRedisTemplate.opsForSet().remove(key,userId);
    }
}
